/*
MIT License

Copyright (c) 2022 chen-chaochen,guo-yichen@Easylosu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
SOFTWARE.

*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/unistd.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>

    #define MAXSOCKET 128
    #define MAXLINK 2048
    #define BUFFSIZE 2048
    typedef  char* LNI;
    LNI losu_socket_start(LNI port);
    LNI losu_socket_open(LNI ip,LNI port);
    LNI losu_socket_stop(LNI ptr);
    LNI losu_socket_getinfo(LNI ptr);
    LNI losu_socket_sendinfo(LNI ptr,LNI str);
    int sockfd[MAXSOCKET], connfd[MAXSOCKET];
    struct sockaddr_in servaddr[MAXSOCKET]; 
    char buff[BUFFSIZE]; 
    char tmp[BUFFSIZE];
    int bool_ptr[MAXSOCKET];
    int freenum(){
        for(int i=0;i<MAXSOCKET;i++){
            if(bool_ptr[i]==0) return i;
        }
        return -1;
    }

    LNI losu_socket_start(LNI port){
        int PORT = atoi(port);
        int PTR = freenum();
        if(PTR<0) return "-1";
        sockfd[PTR] = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd[PTR]==-1)
            return "-1";
        bzero(&servaddr[PTR], sizeof(servaddr[PTR]));
        servaddr[PTR].sin_family = AF_INET;
        servaddr[PTR].sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr[PTR].sin_port = htons(PORT);
        if (bind(sockfd[PTR], (struct sockaddr*)&servaddr[PTR], sizeof(servaddr[PTR]))==-1)
            return "-1";
        if (listen(sockfd[PTR], MAXLINK)==-1)
            return "-1";
        sprintf(tmp,"%d",PTR);
        bool_ptr[PTR]=1;
        return tmp;
    }
    LNI losu_socket_getinfo(LNI ptr){
        int PTR = atoi(ptr);
        if(connfd[PTR]==sockfd[PTR]){
            if (-1 == connfd[PTR])
                return "-1";
            bzero(buff, BUFFSIZE);
            recv(connfd[PTR], buff, BUFFSIZE - 1, 0);
            return buff;
        }
        connfd[PTR] = accept(sockfd[PTR], NULL, NULL);
        if (-1 == connfd[PTR])
            return "-1";
        bzero(buff, BUFFSIZE);
        recv(connfd[PTR], buff, BUFFSIZE - 1, 0);
        return buff;
    }
    LNI losu_socket_sendinfo(LNI ptr,LNI str){
        int PTR = atoi(ptr);
        sprintf(tmp,"%s",str);
        send(connfd[PTR], tmp, strlen(tmp), 0);
        return "0";
    }
    LNI losu_socket_stop(LNI ptr){
        int PTR = atoi(ptr);
        close(connfd[PTR]);
        bool_ptr[PTR]=0;
        return "0";
    }
    LNI losu_socket_open(LNI ip,LNI port){
        int PORT = atoi(port);
        char IP[128];
        int PTR = freenum();
        sprintf(IP,"%s",ip);
        if(PTR<0) return "-1";
        sockfd[PTR] = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd[PTR]==-1)
            return "-1";
        bzero(&servaddr[PTR], sizeof(servaddr[PTR]));
        servaddr[PTR].sin_family = AF_INET;
        inet_pton(AF_INET, IP, &servaddr[PTR].sin_addr);
        servaddr[PTR].sin_port = htons(PORT);
        if(connect(sockfd[PTR], (struct sockaddr*)&servaddr[PTR], sizeof(servaddr[PTR]))==-1)
            return "-1";
        sprintf(tmp,"%d",PTR);
        bool_ptr[PTR]=1;
        connfd[PTR]=sockfd[PTR];
        return tmp;
    }